package it.polimi.swKnights.SWIMv2.ejb.searchEngine;

import it.polimi.swKnights.SWIMv2.ejb.entityBeans.RegisteredUser;

import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 * Session Bean implementation class NormalSearch
 */
@Stateless
public class NormalSearch extends SimpleSearch implements NormalSearchRemote
{

	@Override
	protected String getQueryTextLike(String attribute)
	{
		return "SELECT r FROM RegisteredUser r " + "WHERE r." + attribute
				+ " LIKE :valor";

	}

	@Override
	protected String getQueryTextEqual(String attribute)
	{

		return "SELECT r FROM RegisteredUser r " + "WHERE r." + attribute
				+ "=:valor";
	}

	@Override
	public List<Map<String, String>> lookFor(String query)
	{
		return this.getListOfMapFromGeneralQuery(query, null);
	}

	@Override
	protected void tryUserAttribute(String queryAttribute,
			boolean hasToBeEqual, String value,
			List<UserQueryResult> listQueryRes, Integer ranking, String userName)
	{
		this.checkValidParameterQuery(queryAttribute);
		ResultCategory resultCategory = getResultCategory(queryAttribute);

		Query q;

		if (hasToBeEqual == false)
		{
			q = managerUC.createQuery(this.getQueryTextLike(queryAttribute));
			q.setParameter("valor", "%" + value + "%");
		}
		else
		{
			q = managerUC.createQuery(this.getQueryTextEqual(queryAttribute));
			q.setParameter("valor", value);
		}

		List<?> queryList = q.getResultList();

		for (Object item : queryList)
		{
			RegisteredUser regUser = (RegisteredUser) item;

			UserQueryResult uq = new UserQueryResult(regUser, resultCategory,
					ranking, "");
			listQueryRes.add(uq);
		}
	}

	@Override
	protected void tryAbilityAttribute(boolean hasToBeEqual,
			String ability_name, List<UserQueryResult> listQueryRes,
			Integer ranking, String userName)
	{
		String queryText = "SELECT ru FROM RegisteredUser AS ru WHERE :ability= "
				+ "ANY(SELECT a FROM RegisteredUser u, IN (u.abilities) a WHERE u=ru)";

		Query q;
		q = managerUC.createQuery(queryText);

		q.setParameter("ability", ability_name);

		List<?> queryList = q.getResultList();

		for (Object item : queryList)
		{
			RegisteredUser regUser = (RegisteredUser) item;
			UserQueryResult uq = new UserQueryResult(regUser,
					ResultCategory.ability, ranking, ability_name);
			listQueryRes.add(uq);
		}

	}

	@Override
	protected void tryNameAndSurname(String name, String surname,
			List<UserQueryResult> listQueryRes, Integer ranking, String userName)
	{

		String queryText = "SELECT r FROM RegisteredUser AS r "
				+ "WHERE r.name=:name" + "  AND r.surname=:surname";

		Query q;

		q = managerUC.createQuery(queryText);
		q.setParameter("name", name);
		q.setParameter("surname", surname);

		List<?> queryList = q.getResultList();

		for (Object item : queryList)
		{
			RegisteredUser regUser = (RegisteredUser) item;
			UserQueryResult uq = new UserQueryResult(regUser,
					ResultCategory.name_surname, ranking, "");
			listQueryRes.add(uq);
		}

	}

}
